//
//  SpriteBalloon.m
//  FrogDrying
//
//  Created by Le Thanh Hai on 4/4/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "SpriteBalloon.h"


@implementation SpriteBalloon
-(id)init
{
    if (self = [super init]) {
        balls = [[NSMutableArray alloc] init];
        float offset = -200;
        ColorType color = COLOR_BLUE;
        for (int i = 0; i < 8; i++) {
            BallAnimation *sp = [[BallAnimation alloc] initWithColor:color];
            sp.delegate = self;
            offset += 90;
            sp.position = ccp(offset, 200 + arc4random() % 15);
            [self addChild:sp z:10];
            [balls addObject:sp];
            color++;
            if (color > COLOR_ALL) color = COLOR_BLUE;
        }
    }
    return self;
}

#pragma mark delegate ball animation
-(void)ballExplosion:(BallAnimation *)ball_
{
    CCLOG(@"%@", ball_);
    BallAnimation *firstBall = [balls objectAtIndex:0];
    CGPoint p = firstBall.position;
    for (int i = 0; i < [balls count]; i++) {
        BallAnimation *ball = [balls objectAtIndex:i];
        if ([ball isEqual:ball_]) {
            ball.position = p;
            [balls removeObjectAtIndex:i];
            [balls insertObject:ball atIndex:0];
            [ball resetBall];
            return;
        } else {
            BallAnimation *nextBall = [balls objectAtIndex:i+1];
            [ball runAction:[CCMoveTo actionWithDuration:0.1 position:nextBall.position]];
        }
    }
}

-(void)update:(ccTime)dt
{
    for(int i = 0; i < [balls count]; i++) {
        BallAnimation *sp = [balls objectAtIndex:i];
        
        if ([self checkSpriteOutOfScreen:sp]) {
            [sp resetBall];
            float offsetX = arc4random() % 30 + 100;
            sp.position = ccpAdd([self getLastSprite].position, ccp(-offsetX, 0));
            CGPoint p = sp.position;
            p.y = arc4random() % 60 + 180;
            sp.position = p;
            
            // move last object to first array
            id getSprite = [[[balls objectAtIndex:i] retain] autorelease];
            [balls removeObjectAtIndex:i];
            [balls insertObject:getSprite atIndex:0];
            i = 0;
            return;
        }
        CGPoint point = sp.position;
        point.x += 2;
        sp.position = point;
    }
}

#pragma mark - private function
-(BOOL)checkSpriteOutOfScreen:(CCSprite*)sp
{
    CGSize ss = [[CCDirector sharedDirector] winSize];
    float sw = ss.width + 40;
    if (sp.position.x >= sw) {
        return TRUE;
    } else {
        return NO;
    }
}

-(CCSprite*)getLastSprite {
    CCSprite *temp = [balls objectAtIndex:0];
    for (int i = 1; i < [balls count]; i++) {
        CCSprite *sp = [balls objectAtIndex:i];
        if (temp.position.x > sp.position.x) {
            temp = sp;
        }
    }
    return temp;
}

-(BallAnimation*)getBallAtLocation:(CGPoint)p
{
    for (BallAnimation *ball in balls) {
        if (CGRectContainsPoint([ball rect], p)) {
            return ball;
        }
    }
    return nil;
}
@end
